<?php

namespace App\Admin\Controllers;

use App\Models\Swiper;
use Encore\Admin\Http\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Table;
use Encore\Admin\Show;
use Encore\Admin\Grid\Displayers\Actions;

class SwiperController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '轮播';

    /**
     * Make a grid builder.
     *
     * @return Table
     */
    protected function table()
    {
        $table = new Table(new Swiper());
        $table->model()->withTrashed()->orderBy('display_order', 'desc');

        $table->column('id', 'ID');
        $table->column('display_position', '展示位置')->display(function () {
            return $this->displayPositionText;
        });
        $table->column('image', '图片')->display(function () {
            return imageHtml($this->image, null, 40);
        });
        $table->column('description', '描述');
        $table->column('jump_type', '跳转类型')->display(function () {
            return $this->jumpTypeText;
        });
        $table->column('jump_value', '跳转值');
        $table->column('begin_time', '开始时间');
        $table->column('end_time', '结束时间');
        $table->column('updated_at', '更新于');
        $table->column('状态')->display(function () {
            return $this->trashed() ? '删除' : '正常';
        });

        $table->actions(function (Actions $actions) {
            $row = $actions->row;
            if ($row->trashed()) {
                $actions->disableDelete(true);
                $actions->add(new Restore());
            } else {
                $actions->disableDelete(false);
            }

        });

        return $table;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Swiper::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('display_position', __('Display position'));
        $show->field('image', __('Image'));
        $show->field('description', __('Description'));
        $show->field('jump_type', __('Jump type'));
        $show->field('jump_value', __('Jump value'));
        $show->field('begin_time', __('Begin time'));
        $show->field('end_time', __('End time'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Swiper());

        $form->checkbox('display_position', '展示位置')->options(Swiper::$displayPositionText)->rules('required');
        $form->cloudinaryImage('image', '图片')->rules('required');
        $form->text('description', '说明');
        $form->select('jump_type', '跳转类型')->options(Swiper::$jumpTypeText)->rules('required');
        $form->text('jump_value', '跳转值')->rules('required');
        $form->number('display_order', '展示顺序')->placeholder('越大越靠前')->default(0)->rules('required');
        $form->datetime('begin_time', '起始时间')->default(date('Y-m-d H:i:s'))->rules('required');
        $form->datetime('end_time', '结束时间')->default('2099-12-30')->rules('required');

        $form->saving(function (Form $form) {
            $form->image = attachmentKey($form->image);
        });

        $form->editing(function (Form $form) {
            $model = $form->model();
            $model->image = attachmentUrl($model->image);
        });

        return $form;
    }
}
